package com.webdemo10.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.webdemo10.pojo.User;
import com.webdemo10.service.UserService;
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private SessionFactory sessionFactory;
	public UserServiceImpl(){
		System.out.println("userService");
	}
	@Override
	public void addUser(User user) {
		sessionFactory.getCurrentSession().save(user);
	}
	@Override
	public void deleteUser(User user) {
		sessionFactory.getCurrentSession().delete(user);
	}
	@Override
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().update(user);
	}
	@Override
	public User getUser(int id) {
		User user = (User)sessionFactory.getCurrentSession().get(User.class,id);
		return user;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers() {
		String hql="from User";
		List<User> users = (List<User>) sessionFactory.getCurrentSession().createQuery(hql).list();
		return users;
	}

}
